import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {lister,rem} from '../services/index';


export interface IndexModelState {
 list:any[],
 sea:any[]
}

export interface IndexModelType {
  namespace: 'search';
  state: IndexModelState;
  effects: {
    lister:Effect,
    rem:Effect
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}
const IndexModel: IndexModelType = {
  namespace: 'search',
  state: {
    list:[],
    sea:[]

  },

  //异步action
  effects: {
    *lister({ payload }, { call, put }) {
      let results = yield call(lister,payload&&payload.type,payload&&payload.keyword,payload&&payload.count);
      console.log(results)
      yield put({
        type:'save',
        payload:{
         list:results

        }
      })
    },
    *rem({ payload }, { call, put }) {
      let results = yield call(rem,payload.id);
      console.log(payload)
      yield put({
        type:'save',
        payload:{
         sea:results

        }
      })
    },
  },
  //同步action
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  },
};

export default IndexModel;